﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using Sexybeauty.Core.Inf.LogicEntity;
using Sexybeauty.Core.Inf.Entities;
using Sexybeauty.Framework.Cache;
using Sexybeauty.Framework.Common;

namespace Sexybeauty.Core.Inf.LogicEntity
{
    public abstract class IAccessible<dbEntity, lcEntity>
        where dbEntity : BaseEntity<dbEntity>, new()
        where lcEntity : class
    {
        protected abstract void Covariant(dbEntity dbModel);
        protected abstract dbEntity Inverter(lcEntity model);
        public lcEntity Set(dbEntity dbModel)
        {
            Covariant(dbModel);
            return this as lcEntity;
        }
        public virtual lcEntity Element(Expression<Func<dbEntity, bool>> where)
        {
            try
            {
                var item = new dbEntity().Get(where);
                if (item != null)
                {
                    Covariant(item);
                    return this as lcEntity;
                }
            }
            catch (Exception e)
            {
                LogHelper.Write(CommonLogger.System, LogLevel.Error, e);
            }
            return null;
        }
        public List<lcEntity> Elements(Expression<Func<dbEntity, bool>> where)
        {
            try
            {
                using (Context ctx = new Context())
                {
                    var list = ctx.Set<dbEntity>().Where(where)
                        .ToList();// ctx.a.Where(where).Select(select).ToList();
                    return list.Select(c => this.Set(c)).ToList();
                }
            }
            catch (Exception e)
            {
                LogHelper.Write(CommonLogger.System, LogLevel.Error, e);
            }
            return null;
        }

        public virtual lcEntity PostAdd()
        {
            try
            {
                var item = Inverter(this as lcEntity);
                if (item.PostAdd() > 0)
                {
                    Covariant(item);
                    return this as lcEntity;
                }
            }
            catch (Exception e)
            {
                LogHelper.Write(CommonLogger.System, LogLevel.Error, e);
            }
            return null;
        }
        public virtual lcEntity PostModify()
        {
            try
            {
                var item = Inverter(this as lcEntity);
                if (item.PostModify() > 0)
                {
                    Covariant(item);
                    return this as lcEntity;
                }
            }
            catch (Exception e)
            {
                LogHelper.Write(CommonLogger.System, LogLevel.Error, e);
            }
            return null;
        }
        public virtual lcEntity PostDelete()
        {
            try
            {
                var item = Inverter(this as lcEntity);
                if (item.PostDelete() > 0)
                {
                    Covariant(item);
                    return this as lcEntity;
                }
            }
            catch (Exception e)
            {
                LogHelper.Write(CommonLogger.System, LogLevel.Error, e);
            }
            return null;
        }
    }
}
